#include "Test.Application.h"

#undef  APP_NAME
#define APP_NAME	TestPerfomance
#undef  APP_BASE
#define APP_BASE	TestApp


class APP_NAME : public APP_BASE
{
public:
	APP_NAME (IPlatform *platform, EDevice::type deviceType) :
		APP_BASE( platform, deviceType )
	{}

	void Update (float dt) override
	{
		TestApp::Update( dt );

		ESS()->GetGraphicsEngine()->GetViewport()->Apply();
		ESS()->GetGLStateManager()->ClearColor( color4f( 1.0f ) );
		
		batch.Flush();
		
		ESS()->GetGraphicsEngine()->SwapBuffers();
	}

	void OnEnter () override
	{
		APP_BASE::OnEnter();
		
		BatchMaterial	material;

#	if 0

		TexturePtr	texture;
		resMngr.Load( EResource::TEXTURE, "tex/img", texture );

		material.SetTexture( 0, texture );

		ProgramPtr	program = ESS()->GetGraphicsEngine()->GetShaderManager()->
								GetDefaultProgram( EDefaultShader::COLORED_BLEND_WITH_TEXTURE );

#	elif 0
		
		ProgramPtr	program = ESS()->GetGraphicsEngine()->GetShaderManager()->
								GetDefaultProgram( EDefaultShader::COLORED );

#	elif 1

		ProgramPtr	program = ESS()->GetGraphicsEngine()->GetShaderManager()->
								GetDefaultProgram( EDefaultShader::COLORED );
		
		RenderState::ColorState	cs;
		cs.blend		= true;
		cs.blendFuncSrc	= gl_blend_func::ONE_MINUS_SRC_ALPHA;
		cs.blendFuncDst	= gl_blend_func::SRC_ALPHA;

		material.SetColorState( cs );

#	else
		
		TexturePtr	texture;
		resMngr.Load( EResource::TEXTURE, "tex/img", texture );

		material.SetTexture( 0, texture );

		ProgramPtr	program = ESS()->GetGraphicsEngine()->GetShaderManager()->
								GetDefaultProgram( EDefaultShader::TEXTURED_ALPHA_TEST );

#	endif

		material.SetProgram( program );

		
		const ivec2	res = ESS()->GetGraphicsEngine()->GetViewport()->Size();

		const ivec2	dim( 2 );

		const ivec2	count = res / dim;

		const vec2	size = dim.To<vec2>() / res.To<vec2>() * 2.0f;
		
		batch.SetMaterial< Rect2D >( material );

		for (int y = 0; y <= count.y; ++y)
		{
			//batch.SetMaterial< Rect2D >( material );

			for (int x = 0; x <= count.x; ++x)
			{
				rect	r;

#			if 0
				r.LeftBottom() = vec2(float(x), float(y)) * size - 1.0f;
				r.SetSize( size );
#			elif 0
				r.LeftBottom() = vec2(float(x), float(y)) / count.To<vec2>() * 2.0f - 2.0f;
				r.SetSize( vec2(2.0f) );
#			else
				r.LeftBottom() = vec2();
				r.SetSize( size );
#			endif

				color4u	c( ColorUtils::Random<float,4>() );
				c.A() = 255;
				
				batch.AddBatch( Rect2D( r, rect(0.0f, 0.0f, 1.0f, 1.0f), c ) );
			}
		}

		DEBUG_CONSOLE( (string("Max vertices: ") << string().FormatI( batch.GetVerticesCount(), 3, ' ', 10 )).cstr() );
		DEBUG_CONSOLE( (string("Max batches: ") << string().FormatI( batch.GetBatchesCount(), 3, ' ', 10 )).cstr() );
	}

	void OnExit () override
	{
		batch.Clear();
		APP_BASE::OnExit();
	}
};